package edu.henu.personnel.utils;

import edu.henu.personnel.config.IpConfig;
import edu.henu.personnel.entity.Notice;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: PersonnelProject
 * @description: 文件上传工具
 * @author: zhuyang
 * @create: 2019-05-18 11:08
 */
public class FileUtil {

    public static String imgTargetPrefix = IpConfig.ip + "/filesource/teacherImg/";
    public static String imgPath = IpConfig.storePathPrefix + "/webapps/filesource/teacherImg/";

    public static String fileListTargetPrefix = IpConfig.ip + "/filesource/noticeFile/";
    public static String fileListPath = IpConfig.storePathPrefix + "/webapps/filesource/noticeFile/";

    public static String fileTargetPrefix = IpConfig.ip + "/filesource/downloadfile/";
    public static String filePath = IpConfig.storePathPrefix + "/webapps/filesource/downloadfile/";

    public static String imgUpload(MultipartFile multipartFile, String path) throws IOException {
        String imgName = uploadTool(multipartFile, path);
        return imgTargetPrefix + imgName;
    }

    /**
     * @param path 文件在服务器磁盘中的存储路径
     * @return 返回文件在服务器中的访问路径
     */
    public static String downloadFileUpload(MultipartFile multipartFile, String path) throws IOException {
        String fileName = uploadTool(multipartFile, path);
        return fileTargetPrefix + fileName;
    }

    public static String[] fileListUpload(MultipartFile[] fileList, String storeFilePath, String nid) throws IOException {
        ArrayList<String> nameList = new ArrayList<String>();

        // 获取所有附件的名称，并且存储
        for (MultipartFile appendix : fileList) {
            String filename = uploadTool(appendix, storeFilePath);
            nameList.add(fileListTargetPrefix + nid + "/" + filename);
        }

        String[] fileNameList = new String[6];
        for (int i = 0; i < fileList.length; i++) {
            fileNameList[i] = nameList.get(i);
        }

        return fileNameList;
    }

    /**
     * 存储文件，
     * 并返回文件名称
     */
    public static String uploadTool(MultipartFile multipartFile, String path) throws IOException {
        InputStream inputStream = multipartFile.getInputStream();
        String filename = multipartFile.getOriginalFilename();
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        File desFile = new File(file, filename);
        OutputStream outputStream = new FileOutputStream(desFile);
        byte[] temp = new byte[1024];
        int len = -1;
        while ((len = inputStream.read(temp)) != -1) {
            outputStream.write(temp, 0, len);
        }
        inputStream.close();
        outputStream.close();
        return filename;
    }

    public static List<String> getFilePath(Notice notice) {
        List<String> fileNameList = new ArrayList<>();
        if (notice.getNappendix1() != null) {
            fileNameList.add(notice.getNappendix1());
        }
        if (notice.getNappendix2() != null) {
            fileNameList.add(notice.getNappendix2());
        }
        if (notice.getNappendix3() != null) {
            fileNameList.add(notice.getNappendix3());
        }
        if (notice.getNappendix4() != null) {
            fileNameList.add(notice.getNappendix4());
        }
        if (notice.getNappendix5() != null) {
            fileNameList.add(notice.getNappendix5());
        }
        if (notice.getNappendix6() != null) {
            fileNameList.add(notice.getNappendix6());
        }

        return fileNameList;
    }

    public static List<FilePathAndName> getFilePathAndName(List<String> filePathList) {
        List<FilePathAndName> fileList = new ArrayList<>();

        for (int i = 0; i < filePathList.size(); i ++) {
            String filePath = filePathList.get(i);
            FilePathAndName filePathAndName = new FilePathAndName();

            filePathAndName.setFilePath(filePath);

            String tempName = filePath.trim();
            String filename = filePath.substring(tempName.lastIndexOf("/") + 1);
            filePathAndName.setFileName(filename);

            filePathAndName.setFileIndex(i);

            fileList.add(filePathAndName);
        }

        return fileList;
    }

}
